package com.shanjupay.common.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.IOUtils;

import java.io.*;
import java.util.UUID;

/**
 * @projectName: shanjupay
 * @package: com.shanjupay.common.util
 * @className: QiniuUtils
 * @author: Eric
 * @description:
 * @date: 2023/12/3 21:07
 * @version: 1.0
 */
public class QiniuUtils {

    public static void upload2Qiniu(String accessKey, String secretKey, String bucket, byte[] uploadBytes, String fileName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        //其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //生成上传凭证，然后准备上传
        /*String accessKey = "olKC_O3gxdJwCGQ2nrPlqrnn4wugD8uM36qpJqO_";
        String secretKey = "rLpCOIP33tFGW2bvT7x3AZigUdEnWU_nSlFBZZhK";
        String bucket = "shanjupay-shunxi";*/
        //默认不指定key的情况下，以文件内容的hash值作为文件名，这里建议由自己来控制文件名
        //String key = UUID.randomUUID() + ".png";
        String key = fileName;
        //FileInputStream fileInputStream = null;
        //通常这里得到文件的字节数组
        /*fileInputStream = new FileInputStream(new File("C:\\Users\\MK\\Desktop\\1.png"));
           byte[] uploadBytes = IOUtils.toByteArray(fileInputStream);  // 得到字节数组*/
        // byte[] uploadBytes = "hello qiniu cloud".getBytes("utf‐8");
        // 认证
        Auth auth = Auth.create(accessKey, secretKey);
        // 认证通过后得到token(令牌)
        String upToken = auth.uploadToken(bucket);
        try {
            // 上传文件  key:建议生成不重复的一个唯一值
            Response response = uploadManager.put(uploadBytes, key, upToken);
            System.out.println("put = " + response);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }



    //上传测试
    private static void testUpload() {

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huanan());
        //其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //生成上传凭证，然后准备上传
        String accessKey = "olKC_O3gxdJwCGQ2nrPlqrnn4wugD8uM36qpJqO_";
        String secretKey = "rLpCOIP33tFGW2bvT7x3AZigUdEnWU_nSlFBZZhK";
        String bucket = "shanjupay-shunxi";
        //默认不指定key的情况下，以文件内容的hash值作为文件名，这里建议由自己来控制文件名
        String key = UUID.randomUUID() + ".png";
        FileInputStream fileInputStream = null;
        try {
            //通常这里得到文件的字节数组
            fileInputStream = new FileInputStream(new File("C:\\Users\\MK\\Desktop\\1.png"));
            byte[] uploadBytes = IOUtils.toByteArray(fileInputStream);  // 得到字节数组
            // byte[] uploadBytes = "hello qiniu cloud".getBytes("utf‐8");
            // 认证
            Auth auth = Auth.create(accessKey, secretKey);
            // 认证通过后得到token(令牌)
            String upToken = auth.uploadToken(bucket);
            try {
                // 上传文件  key:建议生成不重复的一个唯一值
                Response response = uploadManager.put(uploadBytes, key, upToken);
                System.out.println("put = " + response);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (IOException ex) {
            //ignore
        }
    }

    public static void main(String[] args) {
        //测试上传
        QiniuUtils.testUpload();
    }
}
